//
//  JMZujiViewController.m
//  VoluntaryStar
//
//  Created by xiaorui on 15/8/31.
//  Copyright (c) 2015年 ios. All rights reserved.
//

#import "JMZujiViewController.h"
#import "JMZujiCell.h"
#import "JMConcernSchoolViewController.h"
#import "JMConcernProfessViewController.h"
#import "JMAppNetworkClient.h"
#import <UIImageView+WebCache.h>
#import "JMBrowserViewController.h"
#import "JMLoginViewController.h"

@interface JMZujiViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    NSString *name;
    NSString *address;
    NSString *school;
    NSString *grade;
    NSString *iconImageUrl;
    NSString *characterString;
}

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;
@end

@implementation JMZujiViewController

- (UITableView *)tableView
{
    if (!_tableView) {
        UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        tableView.delegate = self;
        tableView.dataSource = self;
        tableView.backgroundColor = [UIColor clearColor];
        tableView.backgroundView.backgroundColor = [UIColor clearColor];
        tableView.frame = self.view.bounds;
        _tableView = tableView;
    }
    return _tableView;
}
- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [[NSMutableArray alloc]initWithCapacity:0];
        [_dataArray addObjectsFromArray:@[@[@"关注的学校",@"关注的专业"],
                                          @[@"考试成绩",@"性格测试"],
                                          @[@"学霸成绩",@"夏令营表现"]]];
        
    }
    return _dataArray;
}


-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    self.navigationController.navigationBarHidden = NO;
    self.navigationItem.title = @"成长记录";
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.tableView];

    [JMTools showAlertView:self.view];
    JMConfigure *cf = [JMConfigure sharedConfigureManger];
    if(cf.user){
        NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
        [dict setObject:cf.user.userId forKey:@"user"];
        [dict setObject:cf.user.token forKey:@"token"];
        [JMAppNetworkClient getUserInfoWithInfo:dict complete:^(id data, NSError *error) {
            [JMTools hiddenAlerView:self.view];
            if([data[@"status"] integerValue] == 1){
                [JMTools showAlertView:self.view message:data[@"message"]];
                iconImageUrl = data[@"pic"];
                name =data[@"login"];
                school = data[@"mschool"];
                grade = data[@"grade"];
                characterString = data[@"character"];
                NSIndexSet *indexSet=[[NSIndexSet alloc]initWithIndex:0];
                [self.tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];                }
            else{
                [JMTools showAlertView:self.view message:data[@"message"]];
                JMLoginViewController *vc = [JMLoginViewController new];
                JMNavigationController *nav = [[JMNavigationController alloc]initWithRootViewController:vc];
                [self presentViewController:nav animated:YES completion:^{
                }];
            }
        }];
    }
}
#pragma mark  tableView delegate dasouce

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 1;
    }else if (section == 1){
        return 2;
    }else if(section == 2){
        return 2;
    }else{
        return 1;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString * str=@"ID";
    if(indexPath.section == 0){
        JMZujiCell * cell = [tableView dequeueReusableCellWithIdentifier:str];
        if(!cell){
            cell = [[JMZujiCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];
        }
        cell.nameLabel.text = name;
        cell.addressLabel.text = address;
        cell.shoolLabel.text =school;
        cell.gradeLabel.text = grade;
        if(iconImageUrl){
            [cell.iconImageView sd_setImageWithURL:[NSURL URLWithString:iconImageUrl]];
        }
        return cell;
    }else{
        UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:str];
        if(!cell){
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];
            if(indexPath.section == 1){
                cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            }
        }
        cell.textLabel.text = [[self.dataArray objectAtIndex:indexPath.section - 1] objectAtIndex:indexPath.row];
        cell.textLabel.font = [UIFont systemFontOfSize:UI_SCREEN_WITH *0.0375];
        return cell;

    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 4;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if(indexPath.section == 1 && indexPath.row == 0){
        JMConcernSchoolViewController * vc = [JMConcernSchoolViewController new];
        [self.navigationController pushViewController:vc animated:YES];
    }else if(indexPath.section == 1 && indexPath.row == 1){
        JMConcernProfessViewController *vc = [JMConcernProfessViewController new];
        [self.navigationController pushViewController:vc animated:YES];
    }else if(indexPath.section == 2 && indexPath.row ==1){
        JMBrowserViewController *vc =[JMBrowserViewController new];
        NSDictionary *dict = [[NSDictionary alloc]initWithObjectsAndKeys:characterString,@"character", nil];
        vc.url =[NSURL URLWithString:[JMAppNetworkClient xinggeceshiResultUrl:dict]];
        [self.navigationController pushViewController:vc animated:NO];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.section ==0){
        return UI_SCREEN_WITH *0.3125;
    }else{
        return UI_SCREEN_WITH *0.109475;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return UI_SCREEN_HEIGHT *0.026408;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
